#!/usr/env python
# coding: utf-8

import hunspell
import unittest

class Akan(unittest.TestCase):
  def setUp(self):
    self.hobj = hunspell.HunSpell('ak-GH_test.dic', 'ak-GH.aff')
    self.verb = 'ba'

  def testUnknownWord_come_ShouldFail(self):
    bad = self.hobj.spell('come')
    self.assertFalse(bad, 'come is not supposed to be in the test dictionary')

  def testAkanWord_ba_ShouldPass(self):
    good = self.hobj.spell('ba')
    self.assertTrue(good, 'ba should be in the test dictionary')

  def testAffixRule_365_ForVerbs(self):
    expected_stem = [self.verb]
    stem = self.hobj.stem('me%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('wo%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('ɛ%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('ɔ%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('yɛ%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('mo%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('wɔ%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('mea%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('re%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('a%s' % self.verb)
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('n%s' % self.verb)  # gerund
    self.assertEquals(expected_stem, stem)
    stem = self.hobj.stem('nn%s' % self.verb) # negation
    self.assertEquals(expected_stem, stem)


  def testCoverageUsingRealDictionaryAndCheckMinPrecision(self):
    self.hobj = hunspell.HunSpell('ak-GH.dic', 'ak-GH.aff')
    text = open('ak-GH_test.txt').read()
    tokens = text.split()  #Split using spaces between words
    min_precision = tokens[0]  #First token expected be the 
                               #minimum measured precision
    total = len(tokens)
    found_count = 0
    not_found = set()
    for token in tokens:
      if self.hobj.spell(token.strip(' ,.?;"%\'\r\n\t')):
        found_count += 1
      else:
        not_found.add(token)

    precision = 100*found_count/total
    print ('Correctly identified %d tokens out of %d\n'
           'Precision: %d%%' % (found_count, total, precision))
    print 'Could not find: %s' % '; '.join(not_found)
    self.assertTrue(int(min_precision) <= precision, 
                    'At least %s%% precision expected' % min_precision)

if __name__ == '__main__':
  unittest.main()
